package com.suretech.nms.subagent;

import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.agent.MOGroup;
import org.snmp4j.agent.MOServer;
import org.snmp4j.agent.agentx.subagent.NMSDefaultAgentXSharedMOTable;
import org.snmp4j.agent.mo.NMSMOFactory;
import org.snmp4j.agent.mo.MOColumn;
import org.snmp4j.agent.mo.MOFactory;
import org.snmp4j.agent.mo.MOTable;
import org.snmp4j.agent.mo.MOTableIndex;
import org.snmp4j.agent.mo.MOTableModel;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import java.util.*;

public class NMSSubAgent implements MOGroup {
	private List<AgentScalar> scalares;
	private List<AgentTable> tables;

	public NMSSubAgent(MOFactory moFactory, List<AgentScalar> Scalar_Services,
			List<AgentTable> Table_Services) {
		createMO(moFactory, Scalar_Services, Table_Services);
	}

	protected void createMO(MOFactory moFactory,
			List<AgentScalar> Scalar_Services, List<AgentTable> Table_Services) {
		scalares = Scalar_Services;
		tables = Table_Services;
		for (int i = 0; i < tables.size(); i++) {
			tables.get(i).createAgentEntry(moFactory);
		}
	}

	public void registerMOs(MOServer server, OctetString context)
			throws DuplicateRegistrationException {
		// Scalar Objects
		for (int i = 0; i < scalares.size(); i++) {
			server.register(scalares.get(i), context);
		}

		for (int i = 0; i < tables.size(); i++) {
			server.register(tables.get(i).getAgentEntry(), context);
		}
	}

	public void unregisterMOs(MOServer server, OctetString context) {
		// Scalar Objects
		for (int i = 0; i < scalares.size(); i++) {
			server.unregister(scalares.get(i), context);
		}

		for (int i = 0; i < tables.size(); i++) {
			server.unregister(tables.get(i).getAgentEntry(), context);
		}
	}

	public static MOFactory getTableFactory() {
		return new NMSAgentMOFactory();
	}

	static class NMSAgentMOFactory extends NMSMOFactory {
		public MOTable createTable(OID oid, MOTableIndex indexDef,
				MOColumn[] columns) {
			return new NMSDefaultAgentXSharedMOTable(oid, indexDef, columns);
		}

		public MOTable createTable(OID oid, MOTableIndex indexDef,
				MOColumn[] columns, MOTableModel model) {
			NMSDefaultAgentXSharedMOTable table = (NMSDefaultAgentXSharedMOTable) createTable(
					oid, indexDef, columns);
			table.setModel(model);
			return table;
		}

		// public MOTableModel createTableModel(OID tableOID,
		// MOTableIndex indexDef, MOColumn[] columns) {
		// return new NMSMOTableModel();
		// }
	}
}
